//
//  FivePicsModeView.m
//  XRmenu
//
//  Created by shengchao yang on 11-11-25.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "FivePicsModeView.h"
#import "FivePicsModeViewCell.h"

@implementation FivePicsModeView

-(id) initWithStyle:(NSString*)stylename DishSubViewName:(NSString*)vname
{
    self = [super initWithStyle:stylename DishSubViewName:vname];
    if (self) {
        //在界面上呈现tableview
        NSDictionary *tdict = [[[[[APPSESSION.allconfig objectForKey:@"UI Skin"] objectForKey:stylename] objectForKey:@"Main View"] objectForKey:@"Display Mode View"] objectForKey:@"FivePics Mode View"];
        NSDictionary *tabdict = [[[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"TableView CSS"] objectForKey:[tdict getStringValueAtPath:@"sub tableview/table view style"]];
        if (tdict) {
            [self setTableViewProperty:tabdict Frame:CGRectFromString([tdict getStringValueAtPath:@"sub tableview/table view rect"])];
        }
    }
    return self;
}
#pragma mark 数据更新
//=========================================================== 
// UpdateModeViewDataWithPluPlu
// 更新TableView模式显示的内容
// 通过DisplayModeView被MainView调用
//=========================================================== 
-(void) UpdateModeViewDataWithPluPlu{
    [((UITableView *)[self viewWithTag:kTagOfStartTableView]) reloadData];
    
    @try {
        //滚动到当前位置
        if (APPSESSION.currentPluIndex<[APPSESSION.pluPlus count]) {
            DebugLog(@"index=%d;总的行数=%d", APPSESSION.currentPluIndex, [APPSESSION.pluPlus count]);
            NSIndexPath *indexPath=[NSIndexPath indexPathForRow:APPSESSION.currentPluIndex inSection:0];
            UITableView *atableview=((UITableView *)[self viewWithTag:kTagOfStartTableView]);
            [atableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
        }
        else{
            DebugLog(@"滚动行数越界：index=%d;总的行数=%d", APPSESSION.currentPluIndex, [APPSESSION.pluPlus count]);
        }
    }
    @catch (NSException *exception) {
        DebugLog(@"catch:name=%@; reason=%@", [exception name],[exception reason]);
    }
    @finally {
        DebugLog(@"currentPluIndex=%d", APPSESSION.currentPluIndex);
    }
}

#pragma mark -
#pragma mark Tableview delegate and datasource
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [APPSESSION.pluPlus count];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"LanguageCell";
	FivePicsModeViewCell *cell = (FivePicsModeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil)
	{
		NSString *c = [APPSESSION.allconfig getStringValueAtPath:[NSString stringWithFormat:@"UI Skin/%@/Main View/Display Mode View/FivePics Mode View/sub tableview/table cell style", APPSESSION.uistyle]];
		cell = (FivePicsModeViewCell *)[[[FivePicsModeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier Customer:c] autorelease];
	}
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell setData:indexPath.row];//初始化cell里的数据
    return cell;
}

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.修改当前菜品的下标位置
    [APPSESSION updateCurrentPluIndex:indexPath.row];
    //2.通知主界面修改右边tableview的选中状态
    postN(kMessage_MainView_SelectMainViewPluPlu);
}
@end
